﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using WenSkin.Controls;

namespace WenSkin.Forms
{
    public partial class WenTabControlForm : LeftButtonForm
    {
        public WenTabControlForm()
        {
            InitializeComponent();

            tabControl = new WenTabControl()
            {
                Dock = DockStyle.Fill,
                IconEnable=true,
            };
            tabControl.SelectedIndexChanged += (s, e) =>
            {
                this.Text = $"{DefaultText} - {tabControl.SelectedTab?.Text}";
            };
            tabControl.ControlRemoved += TabControl_ControlRemoved;
            this.Controls.Add(tabControl);
        }

        private void TabControl_ControlRemoved(object sender, ControlEventArgs e)
        {
            e.Control.Dispose();
        }

        private WenTabControl tabControl;

        #region 公有属性
        [Category("Wen"), Description("选项卡点击后默认显示内容"), DefaultValue(null)]
        public string DefaultText { get; set; }

        #endregion

        public void TabPageAdd(Control c, Image image = null)
        {
            TabControlAddEventArgs e = new TabControlAddEventArgs(c)
            {
                Image = image,
            };
            OnTabControlAdd(e);
        }

        protected override void OnLeftMenuClick(LeftMenuItem item)
        {
            base.OnLeftMenuClick(item);
            TabControlAddEventArgs e = new TabControlAddEventArgs(item);
            OnTabControlAdd(e);
        }

        //检查TabPage 是否存在
        public TabPage GetTabPage(string text)
        {
            foreach (TabPage item in tabControl.TabPages)
            {
                if (item.Text == text)
                    return item;
            }
            return null;
        }

        //显示指定页
        public void ShowTabPage(string text)
        {
            ShowTabPage(GetTabPage(text));
        }
        //显示指定页
        public void ShowTabPage(TabPage tab)
        {
            tabControl.SelectedTab = tab;
        }

        #region 委托事件

        protected virtual void OnTabControlAdd(TabControlAddEventArgs e)
        {

            foreach (WenTabPage p in tabControl.TabPages)
            {
                if (p.Text == e.Text)
                {
                    if (p.Controls.Count > 0 && e.Control == null)
                    {
                        e.Control = p.Controls[0];
                    }
                    tabControl.SelectedTab = p;
                    e.TabPage = p;
                    e.Show = true;
                    e.Refresh = false;
                    e.NewAdd = false;     
                }
            }
            if (e.TabPage == null)
            {
                e.TabPage = new WenTabPage(e.Text) { BackColor = Color.Transparent, Image = e.Image ?? e.Item.Image };
            }

            TabControlAdd?.Invoke(this, e);

            if (e.State)
            {
                if (e.NewAdd && !tabControl.TabPages.Contains(e.TabPage))
                {
                    tabControl.TabPages.Add(e.TabPage);
                    tabControl.SelectedTab = e.TabPage;
                    this.Text = $"{DefaultText} - {e.TabPage.Text}";
                }
                if (e.Control != null && e.Refresh)
                {
                    e.TabPage.Controls.Clear();
                    e.Control.Dock = DockStyle.Fill;
                    e.TabPage.Controls.Add(e.Control);    

                    Point mouseOff = new Point(0, 0); //鼠标移动位置变量
                    bool leftFlag = false; //标签是否为左键

                    e.Control.MouseDown += (s, e) =>
                    {
                        if (e.Button == MouseButtons.Left)
                        {
                            var point = this.PointToClient(MousePosition);
                            mouseOff = new Point(-point.X, -point.Y);
                            leftFlag = true;
                            //点击左键按下时标注为true;
                        }
                    };
                    e.Control.MouseMove += (s, e) =>
                    {
                        if (leftFlag)
                        {
                            Point mouseSet = MousePosition;
                            mouseSet.Offset(mouseOff.X, mouseOff.Y);
                            //设置移动后的位置
                            Location = mouseSet;
                        }
                    };
                    e.Control.MouseUp += (s, e) =>
                    {
                        if (leftFlag)
                        {
                            leftFlag = false;
                            //释放鼠标后标注为false;
                        }
                    };
                }
            }
        }
        //声明 API 函数
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
        public class TabControlAddEventArgs : EventArgs
        {
            public TabControlAddEventArgs()
            {
                TabPage = null;
                Control = null;
                Show = false;
                State = true;
                Refresh = true;
                NewAdd = true;
            }
            public TabControlAddEventArgs(string text) : this()
            {
                Item = new LeftMenuItem(text);
                Text = text;
            }
            public TabControlAddEventArgs(Control c) : this(c.Text)
            {
                Control = c;
            }
            public TabControlAddEventArgs(LeftMenuItem item) : this()
            {
                Item = item;
                Text = item.Text;
            }

            private Control control;
            private bool newAdd;
            private string text;
            private Image image;

            public LeftMenuItem Item { get; set; }
            public Control Control { get => control; set { control = value; Refresh = true; } }
            [Category("Wen"), Description("当前选项卡"), DefaultValue(null)]
            public WenTabPage TabPage { get; set; }
            [Category("Wen"), Description("是否已经显示"), DefaultValue(null)]
            public bool Show { get; set; }
            [Category("Wen"), Description("是否继续处理事件"), DefaultValue(null)]
            public bool State { get; set; }
            [Category("Wen"), Description("标签页显示内容"), DefaultValue(null)]
            public string Text
            {
                get => text;
                set
                {
                    text = value;
                    if (TabPage != null)
                        TabPage.Text = value;
                }
            }
            [Category("Wen"), Description("标签页文字显示内容"), DefaultValue(null)]
            public string TabPageText { get => TabPage.Text; set => TabPage.Text = value; }

            [Category("Wen"), Description("是否刷新内容"), DefaultValue(null)]
            public bool Refresh { get; set; }
            [Category("Wen"), Description("是否添加新标签页"), DefaultValue(null)]
            public bool NewAdd { get => newAdd; set { newAdd = value; if (!value) State = false; } }

            [Category("Wen"), Description("标签图标"), DefaultValue(null)]
            public Image Image { get => image; set { image = value; if (TabPage != null) TabPage.Image = value; } }
        }

        #endregion

        #region 委托

        public delegate void TabControlAddEventHandler(object sender, TabControlAddEventArgs e);

        [Category("Wen"), Description("选项卡添加事件")]

        public event TabControlAddEventHandler TabControlAdd;

        #endregion
    }
}
